//
//  ConfigVC.m
//  Mahjong
//
//  Created by 饶首建 on 17/5/22.
//  Copyright © 2017年 com.ikamobile.com. All rights reserved.
//

#import "ConfigVC.h"
#import "MainVC.h"
#import "MatchModel.h"
#import "PlayerModel.h"
#import "PatternModel.h"
#import "RuleRadioCell.h"

//#import "GizUtil.h"

@interface ConfigVC ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *roomNoTF;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTF;
@property (weak, nonatomic) IBOutlet UITableView *ruleTableView;
@property (weak, nonatomic) IBOutlet UITextField *baseTF;
@property (weak, nonatomic) IBOutlet UITextField *ceillingTF;

@property (nonatomic,strong) PatternModel *patternModel;
@property (nonatomic,strong) NSMutableArray *selectedRuleArr;

@end

static NSString *keyMatchCode = @"keyMatchCode";
static NSString *keyNickName = @"keyNickName";
static NSString *keyBase = @"keyBase";
static NSString *keyCeiling = @"keyCeiling";

static NSString *kRuleRadioCell = @"kRuleRadioCell";


@implementation ConfigVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [GizUtil sharedInstance];
    
    NSString *matchCode = [[NSUserDefaults standardUserDefaults]stringForKey:keyMatchCode];
    NSString *nickName = [[NSUserDefaults standardUserDefaults]stringForKey:keyNickName];
    NSString *base = [[NSUserDefaults standardUserDefaults]stringForKey:keyBase];
    NSString *ceiling = [[NSUserDefaults standardUserDefaults]stringForKey:keyCeiling];
    if (matchCode.length) {
        _roomNoTF.text = matchCode;
    }
    if (nickName.length) {
        _nicknameTF.text = nickName;
    }
    if (base.length) {
        _baseTF.text = base;
    }
    if (ceiling.length) {
        _ceillingTF.text = ceiling;
    }
    
    [_ruleTableView registerNib:[UINib nibWithNibName:NSStringFromClass([RuleRadioCell class]) bundle:nil] forCellReuseIdentifier:kRuleRadioCell];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!_patternModel) {
        [PatternModel loadRuleSuccess:^(id model) {
            Log(@"%@",model);
            _patternModel = model;
            _selectedRuleArr = [NSMutableArray new];
            [_ruleTableView reloadData];
        } fail:^(NSString *errorStr) {
            
        }];
    }
}

- (void)loadMatch{
    [MatchModel findOneMatchSuccess:^(id model) {
        MatchModel *mm = model;
        Log(@"%@",mm);
    } fail:^(NSString *errorStr) {
        
    }];
}

- (IBAction)configAction:(id)sender {
    [self joinMatch];
}

- (void)joinMatch{
    NSString *nickName = [_nicknameTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *matchCode = [_roomNoTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *baseStr = [_baseTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString *ceilStr = [_ceillingTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (matchCode.length == 0) {
        [Hud hudText:@"请输入房间号" inView:self.view];
        return;
    }
    if (nickName.length == 0) {
        [Hud hudText:@"请输入您的昵称" inView:self.view];
        return;
    }
    if (baseStr.length == 0) {
        [Hud hudText:@"请输入底分数" inView:self.view];
        return;
    }
    if (ceilStr.length == 0) {
        [Hud hudText:@"请输入最大番数" inView:self.view];
        return;
    }
    
    [[NSUserDefaults standardUserDefaults]setObject:nickName forKey:keyNickName];
    [[NSUserDefaults standardUserDefaults]setObject:matchCode forKey:keyMatchCode];
    [[NSUserDefaults standardUserDefaults]setObject:baseStr forKey:keyBase];
    [[NSUserDefaults standardUserDefaults]setObject:ceilStr forKey:keyCeiling];
    
    NSString *url = [BASE_URL stringByAppendingPathComponent:API_POST_JOIN_MATCH];
    NSDictionary *param = @{
                            @"base":baseStr?baseStr:@1,
                            @"rules":_selectedRuleArr.count?_selectedRuleArr:@[],
                            @"matchCode":matchCode,
                            @"playerName":nickName,
                            @"ceiling":ceilStr
                            };
    [HttpUtil sendRequestURL:url method:POST param:param success:^(id response) {
        NSDictionary *dic = response;
        MainVC *mainVC = [[MainVC alloc]init];
        mainVC.matchModel = [MatchModel parseDic:dic];
        [self presentViewController:mainVC animated:YES completion:^{}];
    } fail:^(NSString *errorStr) {
        [Hud hudText:errorStr inView:self.view];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _patternModel.ruleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RuleRadioCell *cell = [tableView dequeueReusableCellWithIdentifier:kRuleRadioCell forIndexPath:indexPath];
    cell.titleLabel.text = _patternModel.ruleArr[indexPath.row];
    return cell;
}
#pragma mark - UITableViewDeleagte
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40.f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSString *rule = _patternModel.ruleArr[indexPath.row];
    RuleRadioCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([_selectedRuleArr containsObject:rule]) {
        cell.iconImage.image = UIImageWithName(@"mutipleUnSelected");
        [_selectedRuleArr removeObject:rule];
    }else{
        cell.iconImage.image = UIImageWithName(@"mutipleSelected");
        [_selectedRuleArr addObject:rule];
    }
    [tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
